package it.polimi.swKnights.SWIMv2.servlets.administration;

import it.polimi.swKnights.SWIMv2.ejb.administration.BanUserRemote;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidLoginPrivilegesException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.LoginFailException;
import it.polimi.swKnights.SWIMv2.general.Errors;
import it.polimi.swKnights.SWIMv2.servlets.SwimServlet;

import java.io.IOException;

import javax.ejb.EJBException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class endBanProcessData
 */
public class EndBanProcessData extends SwimServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{

		HttpSession session = request.getSession();
		String admin = (String) session.getAttribute("userName");
		String password = (String) session.getAttribute("password");

		String user = request.getParameter("sbanned");

		BanUserRemote banBean = this.lookUp(BanUserRemote.class);

		String message = "";
		String error = "";

		try
		{
			
			banBean.endUserBan(user, admin, password);
			message = "sbanned=" + user;
		}
		catch (LoginFailException e)
		{
			error = "error=" + Errors.session;
		}
		catch (InvalidLoginPrivilegesException e)
		{
			error = "error=" + Errors.notadmin;
		}
		catch (EJBException e)
		{
			error = "error=" + Errors.notbanned;
		}

		response.sendRedirect("./administration?" + message+"&" + error);

	}

}
